import GameCfgData from "../../lib/GameCfgData";
import MyDialogManager from "../../lib/MyDialogManager";
import Sdk from "../../lib/Sdk";
import SoundManager from "../../lib/SoundManger";
import {ui} from "../../ui/layaMaxUI";
import ItemBaseData from "../data/ItemBaseData";
import PlayerData from "../data/PlayerData";
import GridManager from "../GridManager";
export default class PassDialog extends ui.PassDialogUI{
    private mdata;
    private maward=["gold=220"];
    onOpened(_data){
        this.mdata=_data;
        this.addEvents();
        this.fresh();
        if(PlayerData.getIns().currGate>0 && PlayerData.getIns().currGate%10==0){
            this.showAward();
        }
        //if(this.mdata.gold)PlayerData.getIns().addGold(this.mdata.gold);
        Sdk.getIns().showBanner();
        SoundManager.playSound("level_complite.mp3")
        this.gateTxt.text="第"+PlayerData.getIns().currGate+"关";
        PlayerData.getIns().nextGate();
        Sdk.getIns().showInterstitialAd();
        if(PlayerData.getIns().currGate%100==1)GameCfgData.getIns().loadGateCfgDate(PlayerData.getIns().currGate);
    }

    private addEvents(){
        this.colseBtn.on(Laya.Event.CLICK,this,this.colseClick);
        this.nextBtn.on(Laya.Event.CLICK,this,this.nextClick);
    }

    private colseClick(){
        this.close();
        MyDialogManager.getIns().sceneOpen("StartScene.scene",true);
    }

    private nextClick(){
        if(PlayerData.getIns().live<=0){
            MyDialogManager.getIns().sceneOpen("BuyLiveDialog.scene");
            return;
        }
        this.close();
        MyDialogManager.getIns().sceneOpen("GameScene.scene",true);
    }

    private fresh(){
        this.scoreTxt.text=""+GridManager.getIns().score;
        let tstar = GridManager.getIns().getTaskStar();
        for(let i=1;i<=3;i++){
            this.getChildByName("star"+i)["texture"]=tstar>=i?"main/star921.png":"main/StarDisablehardlevel.png";
        }
    }

    private showAward(){
        let  tlv:number=Math.floor(PlayerData.getIns().currGate/10);
        let tgold = 100*tlv;
        let tdata:ItemBaseData=new ItemBaseData(10000000001);
        tdata.num=tgold;
        let tarr=[tdata];
        let tmin = Math.min(tlv,3);
        for(let i=1;i<=tmin;i++){
            tdata=new ItemBaseData(100000+i);
            tdata.num=tlv;
            tarr.push(tdata);
        }
        MyDialogManager.getIns().sceneOpen("AwardDialog.scene",false,tarr);
    }

    private removeEvents(){
        this.colseBtn.off(Laya.Event.CLICK,this,this.colseClick);
        this.nextBtn.off(Laya.Event.CLICK,this,this.nextClick);
    }

    onDisable(){
        this.removeEvents();
        Sdk.getIns().hideBanner();
    }
}